package cn.mugutu.finance.service.system.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import cn.mugutu.finance.domain.system.Menu;
import cn.mugutu.finance.domain.system.Resource;
import cn.mugutu.finance.domain.system.User;
import cn.mugutu.finance.service.system.MenuService;
import cn.mugutu.finance.utils.BeanSortUtil;
import cn.mugutu.finance.utils.ConstantConfig;
import cn.mugutu.finance.dto.BaseResult;

@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements MenuService {

    public List<Menu> findAllMenus(User user) {
        String hql = "SELECT DISTINCT m FROM User u JOIN u.roles r JOIN r.menus m WHERE u = ? AND m.parent is null ";
        //查询一级菜单
        List<Menu> parentMenus = findByHql(hql, user);
        //查询二级菜单
        for (Menu menu : parentMenus) {
            hql = "SELECT DISTINCT m FROM User u JOIN u.roles r JOIN r.menus m WHERE u = ? AND m.parent = ? ORDER BY m.id ";
            List<Menu> children = findByHql(hql, user, menu);
            menu.setChildren(children);
        }
        return parentMenus;
    }

    @Override
    public Resource findByUri(String uri) {
        List list = findByHql("FROM Menu o where o.uri = ? ", uri);
        if (list.size() > 0) {
            return (Resource) list.get(0);
        }
        return null;
    }

    @Override
    public BaseResult saveOrUpdateMenu(Menu menu) {
        if (menu.getId() != null) {
            Menu oldMenu = get(menu.getId());
            oldMenu.setName(menu.getName());
            oldMenu.setUrl(menu.getUrl());
            oldMenu.setIcon(menu.getIcon());
            update(oldMenu);
            return new BaseResult(true, "修改成功");
        } else {
            save(menu);
            return new BaseResult(true, "新增成功");
        }
    }

    @Override
    public boolean judgeMenuIsOneLevelMenu(Long id) {
        if (id == null) {
            return false;
        }
        Menu menu = get(id);
        if (menu != null && menu.getParent() != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Menu> findAllOneLeveMenu() {
        return findByHql("FROM Menu o WHERE o.parent is null");
    }

    @Override
    public List<Menu> findAllSecondLeveMenu() {
        return findByHql("FROM Menu o WHERE o.parent is not null");
    }

    @Override
    public BaseResult updateMenu(Menu menu, User loginUser, HttpSession session) {
        if (menu != null && menu.getId() != null) {
            Menu oldMenu = get(menu.getId());
            if (StringUtils.isNotBlank(menu.getIcon())) {
                oldMenu.setIcon(menu.getIcon());
            }
            if (StringUtils.isNotBlank(menu.getName())) {
                oldMenu.setName(menu.getName());
            }
            update(oldMenu);

            List<Menu> userMenus = findAllMenus(loginUser);
            BeanSortUtil.sort(userMenus, "id", BeanSortUtil.SORT_ASC);

            session.removeAttribute(ConstantConfig.LOGIN_USER_MENUS);
            session.setAttribute(ConstantConfig.LOGIN_USER_MENUS, userMenus);
            return new BaseResult(true, "修改成功,想立即生效请退出后从新登陆.");
        }
        return new BaseResult(false, "修改失败");
    }

}
